Page({

  /**
   * 页面的初始数据
   */
  data: {
    movefzIshow: false,
    concernStatePopIsShow: false,
    concernState: false,
    monitorState: false,
    labelList: [],
    jkJbxxGdxxNewList: [], //股东
    jkJbxxDjgxxNewList: [], //人员
    riskSummary: [], //风险扫描
    corpKey: "", // 企业唯一标识
    textTip: "", // 顶部企业状况
    enterpriseBasicInfo: "", // 企业基本信息
    tabList: [{
      "name": "工商信息",
      "url1": "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/mapDetail/4_new1.png",
      "url2": "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/mapDetail/3.png",
    }, {
      "name": "意愿评分",
      "url1": "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/mapDetail/9_new1.png",
      "url2": "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/mapDetail/10.png",
    }, {
      "name": "测算额度",
      "url1": "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/mapDetail/1_new1.png",
      "url2": "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/mapDetail/2.png",
    }],
    bossInfo: {},
    showGz: false,
    loading: true,
    status: false,
    allInfo: {},
    tabName: '工商信息',
    ed: '',
    yyfsx: '',
    yyfxx: ''
  },
  goMonitor(e) {
    console.log(e, '111')
    let gostate = e.currentTarget.dataset.type
    this.setData({
      monitorState: gostate,
    })
  },
  goConcern(e) {
    console.log(e, '111')
    let gostate = e.currentTarget.dataset.type
    this.setData({
      concernState: gostate,
      concernStatePopIsShow: false
    })
  },
  moveGroup() {
    console.log('222')
    this.setData({
      movefzIshow: true,
      concernState: false,
      concernStatePopIsShow: false

    })
  },
  onClickMove() {
    this.setData({
      movefzIshow: false,
      concernState: false,
      concernStatePopIsShow: false
    })
  },
  concernPop(e) {
    console.log(e, '111')
    let gostate = e.currentTarget.dataset.type
    this.setData({
      concernStatePopIsShow: gostate,
    })
    return false
  },
  concernPopF() {
    this.setData({
      concernStatePopIsShow: false,
      concernState: false
    })
  },
  async getDetails() {
    const res = await getApp().globalData.request(
      "/map/getICInfoByCorpKey", {
        corpKey: this.data.corpKey,
      },
      "POST"
    )
    console.log(res, '企业详情');
    this.setData({
      textTip: this.getTips(res.data.jkJbxxSfxx), // 判断企业的状况
      allInfo: res.data
    });
  },
  // 动态显示tab
  getTips(item) {
    // 根据企业状态点击操作
    if (item.d2Entstatus == "吊销") {
      return `该企业已于${item.writeoffDate}吊销`;
    }
    if (item.d2Entstatus == "注销") {
      return `该企业已于${item.writeoffDate}注销`;
    }
    if (item.d2Entstatus == "吊销后注销") {
      return `该企业已于${item.writeoffDate}注销`;
    }
    if (item.d2Entstatus == "迁出") {
      return "该企业已迁出";
    }
    if (item.d2Entstatus == "其他") {
      return "";
    }
    if (item.d2Entstatus == "个体户撤销") {
      return "";
    }
  },
  // 风险扫描详情
  async goRiskDetail() {
    const isGz = this.data.enterpriseBasicInfo.status || ''
    if (isGz !== '1') {
      wx.showToast({
        title: '请先关注该企业',
        icon: 'none'
      })
      return
    }
    const vipCode = await getApp().globalData.checkVipStatus()
    if (vipCode === 'ordinary' || vipCode === 'notVipBindFI' || vipCode === 'vipBindFI' || vipCode === 'bindGOV' || vipCode === 'visitor') {
      wx.showModal({
        title: '提示',
        content: '该功能为SVIP会员功能，请联系客服了解详情。',
        showCancel: false,
        confirmText: '确定',
        success: function () {}
      })
      return
    }
    wx.navigateTo({
      url: `/sub_monitor/detail/detail?corpCredit=${this.data.enterpriseBasicInfo.corpKey}&name1=${this.data.enterpriseBasicInfo.shortName}&name2=${this.data.enterpriseBasicInfo.d2CorpName}`,
    });
  },
  async selectOne(e) {
    const name = e.currentTarget.dataset.name
    this.setData({
      tabName: name
    })
  },
  // 关注
  async goGz(e) {
    // console.log(e)
    if (e.detail == "1") {
      // 取消关注
      this.setData({
        showGz: true,
      });
    } else {
      // 关注
      const params = {
        companyId: this.data.enterpriseBasicInfo.corpKey,
      };
      const res = await getApp().globalData.request(
        "/api/userApi/subscribeCompany",
        params,
        "POST"
      );
      if (res.data) {
        this.data.enterpriseBasicInfo.status = "1";
        this.setData({
          enterpriseBasicInfo: this.data.enterpriseBasicInfo,
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
        });
      }
    }
  },
  // 取消关注
  goQuxiao() {
    const params = {
      companyId: this.data.enterpriseBasicInfo.corpKey,
    };
    getApp().globalData.request(
      "/api/userApi/unsubscribeCompany",
      params,
      "POST"
    );
    this.data.enterpriseBasicInfo.status = "0";
    this.setData({
      enterpriseBasicInfo: this.data.enterpriseBasicInfo,
    });
  },
  async toRelation(e) {
    const type = e.currentTarget.dataset.type;
    const id = e.currentTarget.dataset.id;
    const vipCode = await getApp().globalData.checkVipStatus()
    if (vipCode === 'ordinary' || vipCode === 'notVipBindFI' || vipCode === 'visitor') {
      wx.showModal({
        title: '提示',
        content: '该功能为VIP会员功能，请联系客服了解详情。',
        showCancel: false,
        confirmText: '确定',
        success: function () {}
      })
      return
    }
    if (type === '1') {
      const params = {
        code: id
      }
      const ryRes = await getApp().globalData.request("/api/relation/findOnceBoss", params, "POST")
      // console.log(ryRes, 'ryRes');
      if (ryRes.data.personName) {
        wx.navigateTo({
          url: `/sub_guanlian/chalaoban_page2/chalaoban_page2?id=${id}`,
        })
      } else {
        wx.showToast({
          title: '暂无相关信息',
          icon: 'none'
        })
      }
    } else {
      const params = {
        corpKey: id
      }
      const qyRes = await getApp().globalData.request("/map/queryWhereExit", params, "POST")
      console.log(qyRes, 'qyRes');
      if (qyRes.data === '1') {
        wx.navigateTo({
          url: `/pages/mapDetail/mapDetail?corpKey=${id}`,
        })
      } else {
        wx.showToast({
          title: '暂无相关信息',
          icon: 'none'
        })
      }

    }
  },
  async toRenyuan(e) {
    const vipCode = await getApp().globalData.checkVipStatus()
    if (vipCode === 'ordinary' || vipCode === 'notVipBindFI' || vipCode === 'visitor') {
      wx.showModal({
        title: '提示',
        content: '该功能为VIP会员功能，请联系客服了解详情。',
        showCancel: false,
        confirmText: '确定',
        success: function () {}
      })
      return
    }
    wx.navigateTo({
      url: '/sub_guanlian/chalaoban_page2/chalaoban_page2?id=' + e.currentTarget.dataset.id,
    })
  },
  // 法人关联图谱
  async toFaRen(e) {
    const vipCode = await getApp().globalData.checkVipStatus()
    if (vipCode === 'ordinary' || vipCode === 'notVipBindFI' || vipCode === 'visitor' || vipCode === 'vipBindFI') {
      wx.showModal({
        title: '提示',
        content: '该功能为SVIP会员功能，请联系客服了解详情。',
        showCancel: false,
        confirmText: '确定',
        success: function () {}
      })
      return
    }
    wx.navigateTo({
      url: `/sub_guanlian/atlas/atlas?id=${this.data.corpKey}&searchType=1`,
    })
  },
  async toQiYe(e) {
    const vipCode = await getApp().globalData.checkVipStatus()
    // if (vipCode === 'ordinary' || vipCode === 'notVipBindFI' || vipCode === 'visitor') {
    //   wx.showModal({
    //     title: '提示',
    //     content: '该功能为VIP会员功能，请联系客服了解详情。',
    //     showCancel: false,
    //     confirmText: '确定',
    //     success: function () {}
    //   })
    //   return
    // }
    wx.navigateTo({
      url: `/sub_guanlian/atlas/atlas?id=${this.data.corpKey}&searchType=2`,
    })
  },
  getList() {
    getApp().globalData.request("/api/relation/findOnceBoss", {
      code: this.data.id
    }, "POST").then(res => {
      console.log(res);
      this.setData({
        bossInfo: res.data
      })
    })
  },
  newInfo() {
    this.setData({
      loading: true
    })
    let userId = wx.getStorageSync('userInfo').userId
    console.log(this.data.corpKey, 'corpKey')
    getApp().globalData.request("/map/getNewInfoByCorpKey", {
      // userId: userId,
      corpKey: this.data.corpKey
    }, "POST").then(res => {
      console.log(this.data.corpKey, '传参')
      console.log(res, '新基本信息传值');
      // res.data.jkJbxxSfxxNew.labelList = res.data.labelList
      res.data.jkJbxxSfxxNew.corpLabelList = res.data.corpLabelList[0].labelList
      res.data.jkJbxxSfxxNew.totalscore = res.data.corpLabelList[0].totalscore
      res.data.jkJbxxSfxxNew.creditRate = res.data.corpLabelList[0].creditRate
      res.data.jkJbxxSfxxNew.d2Kjsbrs = this.generateDisplayRange(Number(res.data.jkJbxxSfxxNew.d2Kjsbrs))
      // d2Kjsbrs
      this.setData({
        enterpriseBasicInfo: res.data.jkJbxxSfxxNew, // 企业基本信息
        jkJbxxGdxxNewList: res.data.jkJbxxGdxxNewList, //股东
        jkJbxxDjgxxNewList: res.data.jkJbxxDjgxxNewList,
        riskSummary: res.data.riskSummary,
        labelList: res.data.labelList,
      })
      // 查评分
      const cId = res.data.jkJbxxSfxxNew.companyId || ''
      if (cId) {
        this.getFen(cId)
      }
      setTimeout(() => {
        this.setData({
          loading: false
        })
      }, 500);
    })
  },
  generateDisplayRange(fieldValue) {
    if (!fieldValue) {
      return '-';
    } else {
      if (fieldValue % 10 == 0) {
        let leftNum = fieldValue
        let right = fieldValue + 10
        let displayRange = `${leftNum}~${right}`;
        return displayRange;
      } else {
        let leftNum = Math.floor(fieldValue / 10) * 10
        let right = Math.ceil(fieldValue / 10) * 10
        let displayRange = `${leftNum}~${right}`;
        return displayRange;
      }
    }


    // // 根据字段值计算左右边界、
    // let leftBoundary = Math.floor(fieldValue / 10) * 10;
    // let rightBoundary = leftBoundary + 10;
    // // 如果字段值是10的倍数，则将右边界增加10 
    // if (rightBoundary % 10 === 0) {
    //   rightBoundary --;
    // }
    // // 将左右边界转换为字符串并用“-”连接起来 
    // let displayRange = `${leftBoundary}~${rightBoundary}`;
    // return displayRange;
  },
  async getFen(item) {
    const res = await getApp().globalData.request("/njcb/yyf_ed", {
      companyId: item
    }, "POST")
    console.log(res, 'ress');
    this.setData({
      ed: res.data ? res.data.ed : '',
      yyfsx: res.data ? res.data.yyfsx : '',
      yyfxx: res.data ? res.data.yyfxx : ''
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      corpKey: options.corpKey
    })
    // 常用服务
    this.getDetails()
    // 基本信息
    this.newInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})